var markedNodeIds = [];
var markerAddin;
function initMarkerddin(obvApi) {
    window.OBV['AddinLibLoader'].loadBuiltInAddinLib('Marker').then(function() {
        console.log('Marker Addin Lib Loaded: ', event);
            /*
        * 通过AddinManager来加载插件库中的MarkerAddin插件
        */
        const addinManager = obvApi.getAddinManager();
        // 插件的ID
        const markerAddinId = 'OBVAddins.Marker';
        // 获取插件
        markerAddin = addinManager.getAddin(markerAddinId);
        if (!markerAddin) {
            // 插件没有加载过，需要加载到平台中
            addinManager.loadAddin(markerAddinId)
                .then(() => {
                    markerAddin = addinManager.getAddin(markerAddinId);                
                })
        }
    });
}

function getMarkerAddin(obvApi) {
    if (!markerAddin) {
        console.error('无法加载插件：', markerAddinId);
    }
    return markerAddin;
}

function getBBox(obvApi, nodeIdArray) {
    if (!nodeIdArray || nodeIdArray.length === 0) {
        return;
    }
    let nodeIdCandidate = nodeIdArray[0];
    for (let i = 0; i < nodeIdArray.length; i++) {
        let found = false;
        let nodeId = nodeIdArray[i];
        for (let j = 0; j < markedNodeIds.length; j++) {
            let markedNodeId = markedNodeIds[j];
            if (markedNodeId.modelId === nodeId.modelId && markedNodeId.dbId === nodeId.dbId) {
                found = true;
                break;
            }
        }
        if (!found) {
            nodeIdCandidate = nodeId;
            break;
        }
    }
    let bbox = obvApi.getObjectsBounds(nodeIdCandidate);
    markedNodeIds.push(nodeIdCandidate);
    return bbox;
}

function createMarker(obvApi, relationItem) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    let bbox = getBBox(obvApi, [relationItem.dbId]);
    if (!bbox) {
        return;
    }
    // let bbox = obvApi.getObjectsBounds(relationItem.nodeIdArray)
    // console.log(relationItem['监测值'])
    // console.log(relationItem['报警值'])
    // console.log(relationItem.ID)
    // console.log(relationItem['监测点'])
    // console.log(relationItem['监测类型'])
    // console.log(relationItem.['累计监测值变化'])
    // console.log('监测点的监测值[' + relationItem['监测值'] + ']大于报警值[' +']，ID['+ relationItem.ID+']，监测点['+relationItem['监测点']+']，监测类型['+ relationItem['监测类型']+']，累计监测值变化['+ relationItem.['累计监测值变化'] +']')
    let tag = relationItem;
    let message =
        'Monitoring Value[' +
        relationItem['监测值'] +
        '] Alarm value[' +
        relationItem['报警值'] +
        ']， Alert！ ID[' +
        relationItem.ID +
        ']，Monitoring point[' +
        relationItem['监测点'] +
        ']， Monitoring type[' +
        relationItem['监测类型'] +
        ']';
    let anchor = '!'; // 'relationItem.relation.anchor'
    let id = new Date(); //relationItem.relation.id + ''
    let position = bbox.center();
    markerAddin.createTextSymbolMarker(position, anchor, tag, message, id, 10, 10);
}

function createMarkerByModel(obvApi, relationItem) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    let bbox = getBBox(obvApi, relationItem.nodeIdArray);
    if (!bbox) {
        return;
    }
    let tag = relationItem;
    let message = relationItem.name;
    let anchor = '!'; // 'relationItem.relation.anchor'
    let id = new Date(); //relationItem.relation.id + ''
    let position = bbox.center();
    markerAddin.createTextSymbolMarker(position, anchor, tag, message, id);
}

function clearMarkers(obvApi) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    markerAddin.clearAnnotations();
    markedNodeIds = [];
}

function recreateMarkers(obvApi, relationItems) {
    // if (!this.isMarkerAddinLoaded) {
    //   return
    // }
    // console.log(relationItems)
    clearMarkers(obvApi);
    relationItems.forEach((relationItem) => {
        createMarker(obvApi, relationItem);
    });

    // toggleMarkerAnchorVisibility();
    // toggleMarkerAnnotationVisibility();
}

function hideMarkers(obvApi) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    markerAddin.hideMarkerAnchors();
    markerAddin.hideAnnotations();
}

function toggleMarkerAnchorVisibility(obvApi, isMarkerAnchorVisible) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    if (isMarkerAnchorVisible) {
        markerAddin.showMarkerAnchors();
    } else {
        markerAddin.hideMarkerAnchors();
    }
}

function toggleMarkerAnnotationVisibility(obvApi, isMarkerAnnotationVisible) {
    let markerAddin = getMarkerAddin(obvApi);
    if (!markerAddin) {
        return;
    }
    if (isMarkerAnnotationVisible) {
        markerAddin.showAnnotations();
    } else {
        markerAddin.hideAnnotations();
    }
}

export {
    initMarkerddin,
    getMarkerAddin,
    getBBox,
    createMarkerByModel,
    createMarker,
    clearMarkers,
    recreateMarkers,
    hideMarkers,
    toggleMarkerAnchorVisibility,
    toggleMarkerAnnotationVisibility,
};
